package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.Userpage;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.example.pinkcat.api.entity.MyFanEntity;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.util.LinkedList;
import java.util.List;


public class FFP_recyleview_adapter extends RecyclerView.Adapter<FFP_recyleview_adapter.ViewHolder>{
    private final Context context;
    int pl_numbers;
    boolean ispage1;
    List<MyFanEntity> myFanOrFollowEntities=new LinkedList<>();
    FFP_recyleview_adapter other_adapter;

    String up_id;
    //构造方法
    public FFP_recyleview_adapter(Context context, int pl_numbers, boolean ispage1, String up_id){
        this.context=context;
        this.pl_numbers=pl_numbers;
        this.ispage1=ispage1;
        this.up_id=up_id;

    }
    public void setOther_adapter(FFP_recyleview_adapter other_adapter){
        this.other_adapter=other_adapter;
    }
    public void setData(List<MyFanEntity> datas){
        this.myFanOrFollowEntities=datas;
        notifyDataSetChanged();
    }

    //内部内
    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView follow_btn;

        RoundedImageView head_img;
        TextView username;
        //寻找声明布局控件
        public ViewHolder(View view) {
            super(view);
            follow_btn=view.findViewById(R.id.followbtn);
            head_img=view.findViewById(R.id.user_head);
            username=view.findViewById(R.id.user_name);
        }
    }
    //创建获取 view  布局
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //获取item内容
        View view;
        view= LayoutInflater.from(context).inflate(R.layout.list_userlist,parent,false);
        return new ViewHolder(view);
    }

    //绑定数据
    @SuppressLint({"CheckResult", "SetTextI18n"})
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //初始化数据
        holder.username.setText(myFanOrFollowEntities.get(position).getUser_name());
        Glide.with(context).load(Uri.parse(myFanOrFollowEntities.get(position).getHead_photo())).into(holder.head_img);
        if (myFanOrFollowEntities.get(position).isIskeep()){
            holder.follow_btn.setText(R.string.unfollow);
            holder.follow_btn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_grey));
        }
        else {
            holder.follow_btn.setText(R.string.follow);
            holder.follow_btn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_pink));
        }
        //关注取关
        holder.follow_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.follow_btn.getText().equals("+ 关 注")){
                    if (GetData.followOrUnfollow(context,myFanOrFollowEntities.get(position).getUser_id(),true)){
                        holder.follow_btn.setText(R.string.unfollow);
                        holder.follow_btn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_grey));
                    }
                }
                else {
                    if (GetData.followOrUnfollow(context,myFanOrFollowEntities.get(position).getUser_id(),false)){
                        holder.follow_btn.setText(R.string.follow);
                        holder.follow_btn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_pink));
                    }
                }
                if (ispage1){
                    GetData.getFollowOrFansUser(context,FFP_recyleview_adapter.this,true,up_id);
                    GetData.getFollowOrFansUser(context,other_adapter,false,up_id);
                }
                else{
                    GetData.getFollowOrFansUser(context,FFP_recyleview_adapter.this,false,up_id);
                    GetData.getFollowOrFansUser(context,other_adapter,true,up_id);
                }
            }
        });
        //头像跳转
        holder.head_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, Userpage.class);
                intent.putExtra("up_id",myFanOrFollowEntities.get(position).getUser_id());
                context.startActivity(intent);
            }
        });

        if (!userStaticMsg.userId.equals(up_id)){//id不同说明在浏览其他人的好友列表
            holder.follow_btn.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return myFanOrFollowEntities.size();
    }
}
